package com.wntms.wy.service.impl;

import com.github.pagehelper.PageHelper;
import com.wntms.wy.dao.CustomerDao;
import com.wntms.wy.pojo.CarrigeContract;
import com.wntms.wy.pojo.CollectionSettle;
import com.wntms.wy.pojo.CustomerAccept;
import com.wntms.wy.pojo.FreightContract;
import com.wntms.wy.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    @Override

    public Boolean isHasFreightContract(String fid) {
        int i = customerDao.freightContractCountFindByFid(fid);

        return i ==1 ? true :false ;
    }

    @Override
    @Transactional
    public Boolean isHasPayment(String fid) {
        FreightContract fc = customerDao.freightContractFindByfid(fid);
        if(fc.getPayment().equals("0")){
            //说明没有代收货款
            return false;
        }else {
            return true;
        }


    }

    @Override
    @Transactional
    public Boolean isPayment(String fid) {

        //先来个判断有没这条记录


            CollectionSettle fc2 = customerDao.collectionSettleFindByfid(fid);

            if (fc2.getIspayment()){
                //说明代收货款已付清
                return true;
            }else {
                return false;
            }



    }

    @Override
    @Transactional
    public Boolean addCustomerAccept(CustomerAccept customerAccept) {
        //controller层调用上面那两个方法说明客户没有代收货款，或已经支付完代收货款。现在可以进行客户验收了
        //cid的前端是没有传过来的，通过fid查询得到，并赋值给customerAccept
        CarrigeContract cc1 = customerDao.carriageContractFindByFid(customerAccept.getFid());
        customerAccept.setCid(cc1.getCid());
        //System.out.println(cc1.getCid() + "..." + "难道获取不到");
        //现在增加客户验收
        int count = customerDao.addCustomerAccept(customerAccept);
        if(count == 1 ){
            FreightContract ff = customerDao.freightContractFindByfid(customerAccept.getFid());
            int i = customerDao.updateFreightStatus3(ff.getFsid());
            return i < 1 ? false :true;
        }else {
            return false;
        }


    }

    @Override
    @Transactional
    public Boolean deleteCustomerAccept(Integer id) {
        int i = customerDao.deleteCustomerAcceptByCaid(id);
        return i < 1 ? false :true;
    }

    @Override
    public Boolean updateCustomerAccept(CustomerAccept customerAccept) {
        int i = customerDao.updateCustomerAccept(customerAccept);
        System.out.println("isme" + i);
        return i < 1 ? false :true;
    }

    @Override
    @Transactional
    public List<CustomerAccept> findAllCustomerAccept(Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<CustomerAccept> list = customerDao.findAllCustomerAccept();
        return list;
    }
}
